#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);
    this->registerWindow = nullptr;
    this->forgetPasswordWindow = nullptr;
    this->username = this->ui->username_lineEdit->text();
    this->password = this->ui->password_lineEdit->text();
    this->ui->radioButton->setChecked(false);
    this->ui->login_pushButton->setEnabled(false);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->installEventFilter(new DragWidgetFilter(this));
}

Login::~Login()
{
    delete ui;
}


void Login::on_radioButton_clicked(bool checked)
{
    this->username = this->ui->username_lineEdit->text();
    this->password = this->ui->password_lineEdit->text();
    if (checked && this->username != nullptr && this->password != nullptr)
    {
        this->ui->login_pushButton->setStyleSheet("background-color: rgb(0, 141, 235)");
        this->ui->login_pushButton->setEnabled(true);
    }
    else
    {
        this->ui->login_pushButton->setStyleSheet("background-color: rgb(158, 218, 255)");
        this->ui->login_pushButton->setEnabled(false);
    }
}

void Login::on_login_pushButton_clicked()
{
    this->chatWindow = new ChatWindow(this);
    this->on_close_toolButton_clicked();
    this->chatWindow->show();
}

void Login::on_close_toolButton_clicked()
{
    this->close();
    if (this->registerWindow != nullptr)
    {
        this->registerWindow->close();
        delete this->registerWindow;
        this->registerWindow = nullptr;
    }
    if (this->forgetPasswordWindow != nullptr)
    {
        this->forgetPasswordWindow->close();
        delete this->forgetPasswordWindow;
        this->forgetPasswordWindow = nullptr;
    }
}

void Login::on_register_pushButton_clicked()
{
    this->registerWindow = new Register();
    this->registerWindow->show();
}

void Login::on_forget_password_pushButton_clicked()
{
    this->forgetPasswordWindow = new ForgetPassword();
    this->forgetPasswordWindow->show();

}
